package com.yc.web.servlets;

import com.aliyun.teaopenapi.models.Config;
import com.google.gson.Gson;
import com.yc.model.JsonModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

/**
 * @author zwei
 * @create 2024-05-07 20:34
 */
@WebServlet("/sendServlet.action")
public class SendServlet extends BaseServlet{

    /**
     * 使用AK&SK初始化账号Client
     * @return Client
     * @throws Exception
     */
    public com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        Config config =new Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId(System.getenv("ACCESS_KEY_ID"))
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret(System.getenv("ACCESS_KEY_SECRET"));
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }



    protected void SendSms(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置响应内容类型
        resp.setContentType("text/html;charset=UTF-8");
        // 实际的逻辑是在这里
        PrintWriter out = resp.getWriter();
        String verificationCode = RandomCodeGenerator();

        JsonModel jm=new JsonModel();
        Gson gson=new Gson();

        String tel=req.getParameter("phoneNumber");
        try {
            com.aliyun.dysmsapi20170525.Client client = createClient();
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setSignName("tomcat登录验证")
                    .setTemplateCode("SMS_465950772")
                    .setPhoneNumbers(tel)
                    .setTemplateParam("{\"code\":\"" + verificationCode + "\"}");
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            // 发送短信
            client.sendSmsWithOptions(sendSmsRequest, runtime);
            out.println("短信发送成功！");
            jm.setCode(1);
        } catch (Exception e) {
            out.println("短信发送失败！");
            e.printStackTrace(out);
            jm.setCode(0);
            super.writeJson(jm,resp);
        }
    }

    public static String RandomCodeGenerator() {
        // 创建一个 Random 对象
        Random random = new Random();
        // 生成六位数的随机验证码
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int digit = random.nextInt(10); // 生成 0 到 9 之间的随机数
            sb.append(digit);
        }
        // 输出随机验证码
        String verificationCode = sb.toString();
        return verificationCode;
    }
}
